﻿using CodeStage.AntiCheat.ObscuredTypes;
using System;
using System.Collections.Generic;
using Util;

public static class ObscuredTool
{
    public static ObscuredInt toInt(this ObscuredString s)
    {
        return ((string)s).toInt();
    }

    public static ObscuredFloat toFloat(this ObscuredString s)
    {
        return ((string)s).toFloat();
    }

    public static string toString(this ObscuredFloat s, int len = 2)
    {
        return ((float)s).toString(len);
    }

    public static string toPercentage(this ObscuredFloat v, int len = 2)
    {
        return ((float)v).toPercentage(len);
    }

    public static string toString(this ObscuredInt s)
    {
        return ((int)s).toString();
    }

    /// <summary>
    /// 比较两个对象是否相等
    /// </summary>
    /// <param name="_str"></param>
    /// <param name="_str1"></param>
    /// <returns></returns>
    public static bool StringSame(this ObscuredString _str, string _str1)
    {
        return string.Compare(_str, _str1, StringComparison.Ordinal) == 0;
    }

    public static int toInt(this ObscuredInt s)
    {
        int v = s;
        return v;
    }

    public static int toInt(this ObscuredFloat s)
    {
        return (int)((float)s);
    }

    public static float toFloat(this ObscuredFloat s)
    {
        float v = s;
        return v;
    }

    public static T toEnum<T>(this ObscuredInt eunm)
    {
        return ((int)eunm).toEnum<T>();
    }

    #region 组

    public static ObscuredString[] toSplit(this ObscuredString s, char gap = StringTool.Split1)
    {
        var vs = ((string)s).toStringList(gap);
        ObscuredString[] vsos = new ObscuredString[vs.Count];
        for (int i = 0; i < vs.Count; i++)
        {
            vsos[i] = vs[i];
        }
        return vsos;
    }

    /// <summary>
    /// 字符串 list
    /// </summary>
    /// <param name="s"></param>
    /// <param name="gap"></param>
    /// <returns></returns>
    public static List<ObscuredString> toStringList(this ObscuredString s, char gap = StringTool.Split1)
    {
        return new List<ObscuredString>(s.toSplit(gap));
    }

    /// <summary>
    /// 字符串矩阵list row行分割符号 pos位置分割符号
    /// </summary>
    /// <param name="s"></param>
    /// <param name="row"></param>
    /// <param name="pos"></param>
    /// <returns></returns>
    public static List<List<ObscuredString>> toStringMatrixList(this ObscuredString s, char row = StringTool.Split2, char pos = StringTool.Split1)
    {
        List<List<ObscuredString>> Matrix = new List<List<ObscuredString>>();
        ObscuredString[] rowatt = s.toSplit(row);
        for (int i = 0; i < rowatt.Length; i++)
        {
            Matrix.Add(new List<ObscuredString>((rowatt[i]).toSplit(pos)));
        }
        return Matrix;
    }

    /// <summary>
    /// 字符串空间list axis1 z纬度, axis2 y纬度, axis3 x纬度
    /// </summary>
    /// <param name="s"></param>
    /// <param name="axis1"></param>
    /// <param name="axis2"></param>
    /// <param name="axis3"></param>
    /// <returns></returns>
    public static List<List<List<ObscuredString>>> toStringSpaceList(this ObscuredString s, char axis1 = StringTool.Split3, char axis2 = StringTool.Split2, char axis3 = StringTool.Split1)
    {
        List<List<List<ObscuredString>>> Matrix = new List<List<List<ObscuredString>>>();
        var xs = s.toSplit(axis1);
        for (int x = 0; x < xs.Length; x++)
        {
            List<List<ObscuredString>> yl = new List<List<ObscuredString>>();
            var ys = xs[x].toSplit(axis2);
            for (int y = 0; y < ys.Length; y++)
            {
                List<ObscuredString> zl = new List<ObscuredString>(ys[y].toSplit(axis3));
                yl.Add(zl);
            }
            Matrix.Add(yl);
        }
        return Matrix;
    }

    /// <summary>
    /// 字符串转intList
    /// </summary>
    public static List<ObscuredInt> toIntList(this ObscuredString s, char gap = StringTool.Split1)
    {
        var satt = s.toSplit(gap);
        int item = 0;
        List<ObscuredInt> list = new List<ObscuredInt>();
        for (int i = 0; i < satt.Length; i++)
        {
            try
            {
                item = satt[i].toInt();
            }
            catch (System.Exception ex)
            {
                debug.log(debug.Level.error_3, "字符串转intList 错误", ex.toString());
            }
            list.Add(item);
        }
        return list;
    }

    /// <summary>
    /// int矩阵 row行分割符号 pos位置分割符号
    /// </summary>
    /// <param name="s"></param>
    /// <param name="row"></param>
    /// <param name="pos"></param>
    /// <returns></returns>
    public static List<List<ObscuredInt>> toIntMatrixList(this ObscuredString s, char row = StringTool.Split2, char pos = StringTool.Split1)
    {
        List<List<ObscuredInt>> Matrix = new List<List<ObscuredInt>>();
        var rowatt = s.toSplit(row);
        for (int j = 0; j < rowatt.Length; j++)
        {
            int item = 0;
            var satt = rowatt[j].toSplit(pos);
            List<ObscuredInt> list = new List<ObscuredInt>();
            for (int i = 0; i < satt.Length; i++)
            {
                try
                {
                    item = satt[i].toInt();
                }
                catch (System.Exception ex)
                {
                    debug.log(debug.Level.error_3, ex);
                }
                list.Add(item);
            }
            Matrix.Add(list);
        }
        return Matrix;
    }

    /// <summary>
    /// Int空间list axis1 z纬度, axis2 y纬度, axis3 x纬度
    /// </summary>
    /// <param name="s"></param>
    /// <param name="axis1"></param>
    /// <param name="axis2"></param>
    /// <param name="axis3"></param>
    /// <returns></returns>
    public static List<List<List<ObscuredInt>>> toIntSpaceList(this ObscuredString s, char axis1 = StringTool.Split3, char axis2 = StringTool.Split2, char axis3 = StringTool.Split1)
    {
        List<List<List<ObscuredInt>>> Matrix = new List<List<List<ObscuredInt>>>();
        var xs = s.toSplit(axis1);
        for (ObscuredInt x = 0; x < xs.Length; x++)
        {
            List<List<ObscuredInt>> yl = new List<List<ObscuredInt>>();
            var ys = xs[x].toSplit(axis2);
            for (ObscuredInt y = 0; y < ys.Length; y++)
            {
                List<ObscuredInt> zl = new List<ObscuredInt>();
                var zs = ys[y].toSplit(axis3);
                for (ObscuredInt z = 0; z < zs.Length; z++)
                {
                    ObscuredInt zv = zs[z].toInt();
                    zl.Add(zv);
                }
                yl.Add(zl);
            }
            Matrix.Add(yl);
        }
        return Matrix;
    }

    /// <summary>
    /// 字符串转FloatList
    /// </summary>
    public static List<ObscuredFloat> toFloatList(this ObscuredString s, char gap = StringTool.Split1)
    {
        var satt = s.toSplit(gap);
        ObscuredFloat item = 0;
        List<ObscuredFloat> list = new List<ObscuredFloat>();
        for (int i = 0; i < satt.Length; i++)
        {
            try
            {
                item = satt[i].toFloat();
            }
            catch (System.Exception ex)
            {
                debug.log(debug.Level.error_3, "字符串转FloatList", ex);
            }
            list.Add(item);
        }
        return list;
    }

    /// <summary>
    /// ObscuredFloat矩阵 row行分割符号 pos位置分割符号
    /// </summary>
    /// <param name="s"></param>
    /// <param name="row"></param>
    /// <param name="pos"></param>
    /// <returns></returns>
    public static List<List<ObscuredFloat>> toFloatMatrixList(this ObscuredString s, char row = StringTool.Split2, char pos = StringTool.Split1)
    {
        List<List<ObscuredFloat>> Matrix = new List<List<ObscuredFloat>>();
        var rowatt = s.toSplit(row);
        for (int j = 0; j < rowatt.Length; j++)
        {
            ObscuredFloat item = 0;
            var satt = rowatt[j].toSplit(pos);
            List<ObscuredFloat> list = new List<ObscuredFloat>();
            for (int i = 0; i < satt.Length; i++)
            {
                try
                {
                    item = satt[i].toFloat();
                }
                catch (System.Exception ex)
                {
                    debug.log(debug.Level.error_3, ex);
                }
                list.Add(item);
            }
            Matrix.Add(list);
        }
        return Matrix;
    }

    /// <summary>
    /// ObscuredFloat空间list axis1 z纬度, axis2 y纬度, axis3 x纬度
    /// </summary>
    /// <param name="s"></param>
    /// <param name="axis1"></param>
    /// <param name="axis2"></param>
    /// <param name="axis3"></param>
    /// <returns></returns>
    public static List<List<List<ObscuredFloat>>> toFloatSpaceList(this ObscuredString s, char axis1 = StringTool.Split3, char axis2 = StringTool.Split2, char axis3 = StringTool.Split1)
    {
        List<List<List<ObscuredFloat>>> Matrix = new List<List<List<ObscuredFloat>>>();
        var xs = s.toSplit(axis1);
        for (int x = 0; x < xs.Length; x++)
        {
            List<List<ObscuredFloat>> yl = new List<List<ObscuredFloat>>();
            var ys = xs[x].toSplit(axis2);
            for (int y = 0; y < ys.Length; y++)
            {
                List<ObscuredFloat> zl = new List<ObscuredFloat>();
                var zs = ys[y].toSplit(axis3);
                for (int z = 0; z < zs.Length; z++)
                {
                    ObscuredFloat zv = zs[z].toFloat();
                    zl.Add(zv);
                }
                yl.Add(zl);
            }
            Matrix.Add(yl);
        }
        return Matrix;
    }

    #endregion 组
}